﻿#include "uimodelyctableview.h"

UiModelYcTableView::UiModelYcTableView(QWidget *parent)
    : UiFilterTableView{parent}
{
    connect(m_addBtn, &QPushButton::clicked, this, &UiModelYcTableView::addYc);
}

void UiModelYcTableView::reload(const QString &modelname, const QString &user)
{
    this->clearData();

    if(modelname.isEmpty()) {
        m_modelName.clear();
        m_user.clear();
        return;
    }

    m_user = user;
    m_modelName = modelname;

    QList<QSharedPointer<IModel>> ycs;
    if(!CWebRequestApi::ins().getModelYcList(ycs, modelname))
        return;

    if(ycs.size() > 0) {
        this->appendData(ycs);
    }
}

void UiModelYcTableView::addYc() {
    if(m_modelName.isEmpty()) {
        ElaMessageBar::error(ElaMessageBarType::Top, "警告", "请先选择操作模型!",2000);
        return;
    }

    QSharedPointer<CRtModelYcInfo> info(new CRtModelYcInfo);
    int max_sn = 0;
    QStringList groups;
    QStringList names;
    for(int row = 0; row < m_model->rowCount(); ++row) {
        CRtModelYcInfo* yxptr = static_cast<CRtModelYcInfo*>(m_model->getDataByIndex(row).data()) ;
        if(!yxptr)
            continue;

        if(!groups.contains(yxptr->grouping))
            groups.append(yxptr->grouping);

        if(!names.contains(yxptr->name))
            names.append(yxptr->name);

        if(yxptr->sn >= max_sn)
            max_sn = yxptr->sn;
    }

    info->model_name = m_modelName;
    info->user = m_user;
    info->sn = max_sn+1;
    info->name = "";

    QVBoxLayout* mainlayout = new QVBoxLayout;
    QVBoxLayout* boxLayout = new QVBoxLayout;

    UiModelSigBaseEdit editw(info, groups,UiModelSigBaseEdit::ECreate);

    int fontSize = 16;
    int lineHeidht = 32;

    QHBoxLayout* hlayout = new QHBoxLayout;
    ElaText tmp_base("基数:");
    tmp_base.setMinimumHeight(lineHeidht);
    tmp_base.setTextPointSize(fontSize);
    ElaSpinBox spx_base;
    spx_base.setValue(0);
    hlayout->addWidget(&tmp_base,1);
    hlayout->addWidget(&spx_base,3);
    boxLayout->addLayout(hlayout);

    ElaText tmp_coef("系数:");
    tmp_coef.setMinimumHeight(lineHeidht);
    tmp_coef.setTextPointSize(fontSize);
    ElaSpinBox spx_coef;
    spx_coef.setValue(0);
    hlayout = new QHBoxLayout;
    hlayout->addWidget(&tmp_coef,1);
    hlayout->addWidget(&spx_coef,3);
    boxLayout->addLayout(hlayout);

    ElaText tmp_alarm("越限告警:");
    tmp_alarm.setMinimumHeight(lineHeidht);
    tmp_alarm.setTextPointSize(fontSize);
    ElaToggleSwitch btn_alarm;
    hlayout = new QHBoxLayout;
    hlayout->addWidget(&tmp_alarm,1);
    hlayout->addWidget(&btn_alarm,3);
    boxLayout->addLayout(hlayout);

    ElaText tmp_up("上限值:");
    tmp_up.setMinimumHeight(lineHeidht);
    tmp_up.setTextPointSize(fontSize);
    ElaSpinBox spx_up;
    spx_up.setValue(0);
    hlayout = new QHBoxLayout;
    hlayout->addWidget(&tmp_up,1);
    hlayout->addWidget(&spx_up,3);
    boxLayout->addLayout(hlayout);

    ElaText tmp_down("下限值:");
    tmp_down.setMinimumHeight(lineHeidht);
    tmp_down.setTextPointSize(fontSize);
    ElaSpinBox spx_down;
    spx_down.setValue(0);
    hlayout = new QHBoxLayout;
    hlayout->addWidget(&tmp_down,1);
    hlayout->addWidget(&spx_down,3);
    boxLayout->addLayout(hlayout);

    ElaText tmp_up_desc("上限描述:");
    tmp_up_desc.setMinimumHeight(lineHeidht);
    tmp_up_desc.setTextPointSize(fontSize);
    ElaLineEdit lab_up_desc;
    lab_up_desc.setMinimumHeight(lineHeidht);
    hlayout = new QHBoxLayout;
    hlayout->addWidget(&tmp_up_desc,1);
    hlayout->addWidget(&lab_up_desc,3);
    boxLayout->addLayout(hlayout);

    ElaText tmp_down_desc("下限描述:");
    tmp_down_desc.setMinimumHeight(lineHeidht);
    tmp_down_desc.setTextPointSize(fontSize);
    ElaLineEdit lab_down_desc;
    lab_down_desc.setMinimumHeight(lineHeidht);
    hlayout = new QHBoxLayout;
    hlayout->addWidget(&tmp_down_desc,1);
    hlayout->addWidget(&lab_down_desc,3);
    boxLayout->addLayout(hlayout);

    ElaText tmp_count("告警启动时间:");
    tmp_count.setMinimumHeight(lineHeidht);
    tmp_count.setTextPointSize(fontSize);
    ElaSpinBox spx_count;
    spx_count.setValue(60);
    hlayout = new QHBoxLayout;
    hlayout->addWidget(&tmp_count,1);
    hlayout->addWidget(&spx_count,3);
    boxLayout->addLayout(hlayout);

    QGroupBox* box = new QGroupBox;
    box->setTitle("遥测属性编辑");
    box->setLayout(boxLayout);

    QWidget* wtmp = new QWidget;
    hlayout = new QHBoxLayout;
    hlayout->addWidget(box);
    wtmp->setLayout(hlayout);

    mainlayout->addWidget(&editw);
    mainlayout->addWidget(wtmp);

    UiCustomDialog* dialog = new UiCustomDialog;
    dialog->resize(500, 400);
    dialog->setConten(mainlayout);
    while(dialog->exec() == UiCustomDialog::Accepted) {
        editw.save();

        if(names.contains(info->name)){
            ElaMessageBar::error(ElaMessageBarType::Top, "错误", "信号名字重复,请重新输入!",2000);
            continue;
        }

        if(info->name.isEmpty() || info->descr.isEmpty() || info->grouping.isEmpty()) {
            ElaMessageBar::error(ElaMessageBarType::Top, "错误", "信息不能为空,请检查!",2000);
            continue;
        }

        info->base = spx_base.value();
        info->coef = spx_coef.value();
        info->en_alarm = btn_alarm.getIsToggled();
        info->alarm_up_limit = spx_up.value();
        info->alarm_down_limit = spx_down.value();
        info->alarm_up_limit_desc = lab_up_desc.text();
        info->alarm_down_limit_desc = lab_down_desc.text();
        info->alarm_time_count = spx_count.value();

        if(!CWebRequestApi::ins().addYc(info)) {
            ElaMessageBar::error(ElaMessageBarType::Top, "错误", "添加遥测失败",2000);
            break;
        }

        this->appendData(info);
        ElaMessageBar::success(ElaMessageBarType::Top, "完成", "添加遥测成功",1500);
        break;
    }
    dialog->deleteLater();
}
